@extends('layout.cart')
@section('title','我的购物车')
@section('bodier')
 

<form action="{{url('/orders')}}" method='post'>

  {{csrf_field()}}
   <div class="page-main">
    <div class="container">
      <div id="J_cartLoading" class="cart-loading loading hide">
        <div class="loader"></div>
      </div>
      @if($kong != null)
      <div id="J_cartEmpty" class="cart-empty">
        <h2>您的购物车还是空的！</h2>

       <a class="btn btn-primary btn-shoping J_goShoping" href="/" >马上去购物</a>
      </div>
      @else
      <div class="" id="J_cartBox">
        <div class="cart-goods-list">
          <div class="list-head clearfix">
            <div class="col col-check">
              <i id="J_selectAll" class="iconfont icon-checkbox icon-checkbox-selected" >√</i>全选
            </div>

            <div class="col col-img">
              &nbsp;
            </div>

            <div class="col col-name">
              商品名称
            </div>

            <div class="col col-price">
              单价
            </div>

            <div class="col col-num">
              数量
            </div>

            <div class="col col-total">
              小计
            </div>

            <div class="col col-action">
              操作
            </div>
          </div>


          @foreach($cartinfo as $k=>$v)
          <div id="J_cartListBody" class="list-body">
          <!-- 商品 -->
          
            <div class="item-box">
              <div class="item-table J_cartGoods">
                <div class="item-row clearfix">
                  <div class="col col-check">
                    <i data-status="1" class="iconfont icon-checkbox icon-checkbox-selected J_itemCheckbox">√</i>
                  </div>

                  <div class="col col-img">
                    <a target="_blank" href="#"><img width="80" height="80" src="{{$v['guige_image']}}" alt="" /></a>
                    <input type="hidden" name = "goodspic[{{$k}}]" value="{{$v['guige_image']}}">
                    <input type="hidden" name = "g_id[{{$k}}]" value="{{$v['g_id']}}">
                  </div>

                  <div class="col col-name">
                    <div class="tags"></div>

                    <h3 class="name">

                    <a target="_blank" href="">

                    {{$v['guige_proname']}}  {{$v['color']}} {{$v['size']}}

                    <input type="hidden" name ='jiancheng[{{$k}}]' value ="{{$v['guige_proname']}}  {{$v['color']}} {{$v['size']}}">

                    </a>

                    </h3>

                    <p class="desc"><span>适配机型：</span> {{$v['guige_proname']}}</p>
                  </div>

                  <div class="col col-price">
                    


                      <span  >
                              {{$v['price']}}
                              
                      </span>元
                     <input type="hidden" name = "price[{{$k}}]" value="{{$v['price']}}">
                  </div>

                  <div class="col col-num">
                    <div class="change-goods-num clearfix J_changeGoodsNum">
                      <a class="J_minus reduce_num" href="javascript:;"><i class="iconfont">-</i></a> 

                      <input class="amount"  value="{{$v['num']}}" name="number[{{$k}}]" tyep="text" />
                 
                  
                       <a class="J_plus add_num" href="javascript:;" ><i class="iconfont">+</i></a>
                    </div>
                  </div>

                  <div class="col col-total xiaoji">
                     <span>@if(array_key_exists('package',$v))
                              {{$v['price'] + $v['package']->xprice}}
                              @else
                              {{$v['price']}}
                              @endif</span>元
                     

                    <p class="pre-info"></p>
                  </div>

                  <div class="col col-action">
                     <input type="hidden" name = "cartdel" value = "{{$v['g_id']}}" id ="cartdel">
                    <a class="del J_delGoods" title="删除" href="javascript:;" name=" "><i class="iconfont"></i></a>
                  </div>

                </div>
              </div>
            </div>
          
          <!-- 套餐 -->
          @if(array_key_exists('package',$v))
            <div class="item-box">
            
              <div  class="item-table J_cartGoods">
                <div class="item-row clearfix">
                  <div class="col col-check">
                    <i class="iconfont icon-checkbox icon-checkbox-selected J_itemCheckbox">√</i>
                  </div>

                  <div class="col col-img">
                    <a target="_blank" href="#"><img width="80" height="80" src="{{$v['package']->packageimage}}" alt="" /></a>
                  </div>

                  <div class="col col-name">
                    <div class="tags"></div>

                    <h3 class="name"><a target="_blank" href="#">{{$v['package']->tname}}</a></h3>
                      <input type="hidden" name = 'tname[{{$k}}]' value="{{$v['package']->tname}}">
                    <p class="desc"><span>适配机型：</span>{{$v['package']->packagepro}}</p>
                  </div>

                  <div class="col col-price taocan">
                    {{$v['package']->xprice}}元
                    <p class="pre-info">原价 {{$v['package']->yprice}}元</p>

                  </div>

                  <div class="col col-num">
                    
                     1 
                   
                  </div>

                  <div class="col col-total">
                 
                  <!--  <span id = "xprice" >{{$v['package']->xprice}}</span> 元
                    <p class="pre-info">原价 {{$v['package']->yprice}}元</p> -->
                  </div>
<!-- 
                  <div class="col col-action">
                    <a class="del J_delGoods" title="删除" href="javascript:void(0);" data-msg="确定删除吗？" id="1161900015_1_buy"><i class="iconfont"></i></a>
                  </div> -->
                </div>
              </div>
            
            
              <div class="item-sub-box">

              <!-- 套内商品遍历 -->
               @foreach($v['packagegoods'] as $kkk=>$vvv)
                <div class="item-table">
                  <div class="item-row clearfix">
                    <div class="col col-img">
                      <a target="_blank" href="#">

                      <img width="60" height="60" src="{{$vvv->image}}" alt="{{$vvv->things}}" />
                    

                      </a>
                    </div>

                    <div class="col col-name">
                      <h3 class="name">
                        <a target="_blank" href="#" >
                        {{$vvv->proname}}
                       
                        </a>
                      </h3>
                    </div>

                    <div class="col col-price"></div>

                    <div class="col col-num">
                      1
                    </div>

                    <div class="col col-total"></div>

                    <div class="col col-action"></div>
                  </div>
                </div>
                @endforeach
              </div>
            </div>
            @else
              <input type="hidden" name = "tname[{{$k}}]" value = "无套餐">
           @endif
           
          <!-- </div> -->
        </div><!-- 加价购 -->
@endforeach
       
        <div id="J_cartBar" class="cart-bar clearfix cart-bar-fixed">
          <div class="section-left">
            <a class="back-shopping J_goShoping" href="">继续购物</a> 
            <span class="cart-total">共 <i id="J_cartTotalNum">2</i> 件商品，已选择 <i id="J_selTotalNum">2</i> 件</span> <span id="J_coudanTip" class="cart-coudan hide">，还需 <i id="J_postFreeBalance"></i> 元即可免邮费 
            <a id="J_showCoudan" href=""  name="J_showCoudan">立即凑单</a></span>
        </div>
        <span class="total-price">合计（不含运费）：<em id="J_cartTotalPrice">{{$total}}</em>元</span>
          <input type="hidden" name='zongjia' value = '{{$total}}' id = 'zongjia' >
        
         <input type="submit" id="J_goCheckout" class="btn btn-a btn btn-primary" value='去结算'>

          <div id="J_noSelectTip" class="no-select-tip hide">
            请勾选需要结算的商品
          </div>
        </div>
      </div>
    </div>
  </div>

 @endif


<script type="text/javascript">

$(function(){
  //获取套餐的价格
  var taocan = $('#xprice').html();
  //alert(taocan);
  //减少
  $(".reduce_num").click(function(){
    var amount = $(this).parent().find(".amount");
    //alert(amount.val());
    if (parseInt($(amount).val()) <= 1){
      alert("商品数量最少为1");
    } else{
      $(amount).val(parseInt($(amount).val()) - 1);
    }
    //小计
    var subtotal = parseFloat($(this).parent().parent().parent().find(".col-price span").text()) * parseInt($(amount).val());
    //alert(subtotal);
    $(this).parent().parent().parent().find(".xiaoji span").text(subtotal);
    //总计金额
    var total = 0;
    $(".xiaoji span").each(function(){
      total += parseFloat($(this).text());
    });

    $("#J_cartTotalPrice").text(total);
    $('#zongjia').val(total);
  });

  //增加
  $(".add_num").click(function(){
    var amount = $(this).parent().find(".amount");
    $(amount).val(parseInt($(amount).val()) + 1);
    //alert()
    //小计
    var subtotal = parseFloat($(this).parent().parent().parent().find(".col-price span").text()) * parseInt($(amount).val());
    //alert(subtotal);
    $(this).parent().parent().parent().find(".xiaoji span").text(subtotal);
    //总计金额
    var total = 0;

    $(".xiaoji span").each(function(){
      total += parseFloat($(this).text());
    });

    $("#J_cartTotalPrice").text(total);
    $('#zongjia').val(total);
  });

 

 
});



  $(function(){

         $('.J_delGoods').each(function(){
            $(this).click(function(){
              //获取此商品的单价
              var  danjia = $(this).parents().find('.xiaoji span').html();
              //修改总价格
              var total = $("#J_cartTotalPrice").text();
                total -= parseFloat(danjia);
               $("#J_cartTotalPrice").text(total);
              var gg = $(this).prev('input[name = cartdel]').val();
              //获取上一级的值发送ajax
              $.get('/home/cart/cartdel',{g_id:gg});

              $(this).parents('.list-body').remove();

              if(total == 0){location.assign('/home/cart/cartuser');}

      })

    })

})










  </script>
</form>
@endsection